<?php

class Country_Plugin_Menus
{
  public function onMenuInitialize_CountryLinksJoinLeave($row)
  {
    $viewer  = Engine_Api::_()->user()->getViewer();
    $request = Zend_Controller_Front::getInstance()->getRequest();
    if ($viewer->level_id == 1) return false;
    
	if( Engine_Api::_()->core()->hasSubject() ) {
       // Get subject
       $subject = Engine_Api::_()->core()->getSubject();
      
    }
	if(!$subject->getIdentity()) return false;
		
	$memberTable = Engine_Api::_()->getDbtable('countrymembers','country');
    $status = $memberTable->getStatusMember($viewer->user_id, $subject->country_id);
	
    $route = array();
	$route['route']  = 'default';
	$route['icon'] = 'application/modules/Core/externals/images/loadmore.png';
	$route['class'] = 'smoothbox';
	if($status == Country_Model_DbTable_Countrymembers::NOMEMBER){
		$route['params'] = array(
			'action' => 'join',
			'module' => 'country',
			'controller' => 'index',
            'format'    =>'smoothbox',
            'id'        => $subject->country_id
		);
		$route['label'] = 'Join This Country';
	}else if($status == Country_Model_DbTable_Countrymembers::MEMBER){
		$route['params'] = array(
			'action' => 'leave',
			'module' => 'country',
			'controller' => 'index',
            'format'    =>'smoothbox',
            'id'        => $subject->country_id
		);
		$route['label'] = 'Leave This Country';
	}else if ($status == Country_Model_DbTable_Countrymembers::INVITE){
	    $route['icon'] = 'application/modules/Country/externals/images/manage.png';
		$route['params'] = array(
			'action' => 'join',
			'module' => 'country',
			'controller' => 'index',
            'format'    =>'smoothbox',
            'id'        => $subject->country_id
		);
		$route['label'] = 'Accept this request';
	} else {
	   return false;
	}

    return $route;
  }
  
  
  public function onMenuInitialize_CountryLinksManage($row)
  {
    $viewer  = Engine_Api::_()->user()->getViewer();
    if (!$viewer || empty($viewer->user_id))  return false;
    
    $request = Zend_Controller_Front::getInstance()->getRequest();

	if( Engine_Api::_()->core()->hasSubject() ) {
       // Get subject
       $subject = Engine_Api::_()->core()->getSubject();
      
    }
	if(!$subject->getIdentity()) return false;
	
    $manageT = Engine_Api::_()->getDbtable('managers','country');
    $admin = $manageT->isAdmin($subject->country_id,$viewer->user_id);
    if (!$admin || empty($admin)) return false;
   	
	$route['route']  = 'default';
	$route['icon'] = 'application/modules/Country/externals/images/manage.png';
	$route['params'] = array(
			'action' => 'index',
			'module' => 'country',
            'controller' => 'manage',
            'cid'        => $subject->country_id
		);
	$route['label'] = 'Manager Spot';
	

    return $route;
  }
  
  
  public function onMenuInitialize_CountryLinksInvite($row)
  {
    $viewer  = Engine_Api::_()->user()->getViewer();
    $request = Zend_Controller_Front::getInstance()->getRequest();

	if( Engine_Api::_()->core()->hasSubject() ) {
       // Get subject
       $subject = Engine_Api::_()->core()->getSubject();
      
    }
	if(!$subject->getIdentity()) return false;
	
    	
	$memberTable = Engine_Api::_()->getDbtable('countrymembers','country');
    $status = $memberTable->getStatusMember($viewer->user_id, $subject->country_id);
	
    if($status == Country_Model_DbTable_Countrymembers::MEMBER){
		$route['route']  = 'default';
    	$route['icon'] = 'application/modules/User/externals/images/friends/request.png';
    	$route['class'] = 'smoothbox';
    	$route['params'] = array(
    			'action' => 'invite',
                'module' => 'country',
                'controller' => 'index',
                'country_id' => $subject->country_id
    		);
    	$route['label'] = 'Invite Member';
	}else if ($status == Country_Model_DbTable_Countrymembers::INVITE){
		$route['route']  = 'default';
	    $route['icon'] = 'application/modules/Core/externals/images/loadmore.png';
	    $route['class'] = 'smoothbox';
        $route['params'] = array(
			'action' => 'reject',
			'module' => 'country',
			'controller' => 'index',
            'format'    =>'smoothbox',
            'id'        => $subject->country_id
		);
		$route['label'] = 'Reject this request';
	} else {
	   return false;
	}

    return $route;
  }
}